package Template;

import javax.swing.*;
import java.awt.event.*;

public class MyDialog extends JDialog {
    private JPanel contentPane;
    private JButton buttonOK;
    private JButton buttonCancel;
    private JTextField textField1;
    private JTextField textField2;
    private JTextField textField3;
    private JRadioButton activity;
    private JRadioButton fragment;
    private JRadioButton fragmentList;
    private JTextField module;

    private DialogCallBack mCallBack;

    public MyDialog(DialogCallBack callBack) {
        this.mCallBack = callBack;
        setTitle("Mvp Create Helper");
        setContentPane(contentPane);
        setModal(true);
        getRootPane().setDefaultButton(buttonOK);
        setSize(600, 400);
        setLocationRelativeTo(null);

        activity.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (fragment.isSelected()) {
                    fragment.setSelected(false);
                    fragmentList.setSelected(false);
                }
            }
        });

        fragment.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (activity.isSelected()) {
                    activity.setSelected(false);
                    fragmentList.setSelected(false);
                }
            }
        });

        fragmentList.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (activity.isSelected()) {
                    activity.setSelected(false);
                    fragment.setSelected(false);
                }
            }
        });

        buttonOK.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if (!activity.isSelected() && !fragment.isSelected() && !fragmentList.isSelected()) {
                    return;
                }
                onOK();
            }
        });

        buttonCancel.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                onCancel();
            }
        });
        // call onCancel() when cross is clicked
        setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                onCancel();
            }
        });

        // call onCancel() on ESCAPE
        contentPane.registerKeyboardAction(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                onCancel();
            }
        }, KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
    }

    private void onOK() {
        // add your code here
        if (null != mCallBack){
            if (activity.isSelected()) {
                mCallBack.ok(textField1.getText().trim(), textField2.getText().trim(), textField3.getText().trim(), module.getText().trim(), 1);
            }
            if (fragment.isSelected()) {
                mCallBack.ok(textField1.getText().trim(), textField2.getText().trim(), textField3.getText().trim(), module.getText().trim(), 2);
            }
            if (fragmentList.isSelected()) {
                mCallBack.ok(textField1.getText().trim(), textField2.getText().trim(), textField3.getText().trim(), module.getText().trim(), 3);
            }
        }
        dispose();
    }

    private void onCancel() {
        // add your code here if necessary
        dispose();
    }

//    public static void main(String[] args) {
//        MyDialog dialog = new MyDialog(new DialogCallBack() {
//            @Override
//            public void ok(String author, String moduleName) {
//                System.out.print("成功啦！！！！！！ " + "author:" + author + "moduleName" + moduleName);
//            }
//        });
//        dialog.pack();
//        dialog.setVisible(true);
//        System.exit(0);
//    }

    public interface DialogCallBack{
        void ok(String author, String className, String path, String moduleName, int position);
    }

}
